TC_EVENT_CONFIG = {
    TYPE: '',
    BINDING_URI: base_url + 'cms/cp/event/bindingbytype/',
    EDITPANEL_URI: base_url + 'cms/cp/event/editpanel/',
    COMMIT_URI: base_url + 'cms/cp/event/oncommit/',
    DELETE_URI: base_url + 'cms/cp/event/ondelete/'
};
var TC_EVENT = (function() {

    var oTable;
    var that = this;
    return {
        onCommit: function(Url, Params, Id, callback) {
            /* Commit data l�n v�o DB, Id>0 l� Update, Id=0 ho?c ko c� l� Insert */
            backend({
                url: Url,
                data: {
                    Id: Id,
                    Params: Params
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {
                            sticky: true,
                            theme: 'growl-error',
                            header: 'Error!'
                        });
                    } else {
                        $.jGrowl(rsdata.message);
                        if (typeof callback == 'function') {
                            callback(rsdata);
                        }
                    }
                }
            }).call();
        },
        createtable: function() {
            oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [{
                        mData: "_id",
                        sWidth: "40px",
                        sClass: 'gridAction',
                        mRender: function(value, type, datarow) {
                            var str = '';
                            str += '<ul class="table-controls">';
                            //str += '<li><a href="#" class="tip" onclick="" title="Add entry" ><i class="icon-plus"></i></a> </li>'+
                            str += '<li><a href="#" class="tip" onclick="TC_EVENT.onEditItem(' + value + ')" title="Edit entry:' + value + '" ><i class="icon-pencil"></i></a> </li>';
                            str += '<li><a href="#" class="tip" onclick="TC_EVENT.onDeleteItem(' + value + ')" title="Remove entry:' + value + '" ><i class="icon-trash"></i></a> </li>';
                            str += '</ul>';
                            return str;
                        }
                    }, {
                        mData: "_thumb",
                        sWidth: "40px",
                        sClass: 'gridThumb',
                        mRender: function(value, type, datarow) {
                            if ((!value || /^\s*$/.test(value))) return '';
                            if (value.startsWith('/data/image/')) value = value.replace('/data/image/', '/data/thumbs/image/');
                            return '<img src="' + value + '" title="Thumb"/>';

                        }
                    },

                    {
                        mData: "_name"
                    },

                    {
                        mData: "_position",
                        sWidth: "60px"
                    },

                    {
                        mData: "_status",
                        sWidth: "60px",
                        sClass: 'gridStatus',
                        mRender: function(value, type, datarow) {
                            var str =
                                '<div>' +
                                '<div onclick="TC_EVENT.onStatusChange(this,' + datarow._id + ')" class="icheckbox' + (value === "true" ? " checked" : "") + '"></div>' +
                                '<span>' + (value === "true" ? "Enable" : "Disable") + '</span>'
                            '</div>';
                            return str;
                        }
                    }, {
                        mData: "_insert",
                        sWidth: "120px"
                    },

                    {
                        mData: "_update",
                        sWidth: "120px"
                    }
                ],
                sServerMethod: "POST",
                iDisplayLength: 10,
                bProcessing: true,
                bServerSide: true,
                bPaginate: false,
                sAjaxSource: TC_EVENT_CONFIG.BINDING_URI + TC_EVENT_CONFIG.TYPE,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {
                        "sFirst": "First",
                        "sLast": "Last",
                        "sNext": ">",
                        "sPrevious": "<"
                    }
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onInit: function() {
            this.createtable();
        },
        onAddItem: function() {
            this.onEditItem(0);
        },
        onEditItem: function(Id) {
            if (PENDING)
                return false;
            $('#_edit_panel_insite').show().html('<div class="base_loading" style="display: block;height: 60px"></div>');
            $('#viewport').hide();
            backend({
                'url': TC_EVENT_CONFIG.EDITPANEL_URI + TC_EVENT_CONFIG.TYPE,
                'data': {
                    'Id': Id
                },
                'callback': function(rsdata) {
                    if (rsdata.result < 0) {
                        $('#_edit_panel_insite').html('<span style="color:red">' + rsdata.message + '</span>');
                    } else {
                        $('#_edit_panel_insite').html(rsdata.htmlreponse);
                        $('#_form_edit_panel select').select2({
                            minimumResultsForSearch: -1
                        });
                        addEditorBasic('_content');


                    }
                }
            }).call();
        },
        onSave: function() {
            $('#_content').val(CKEDITOR.instances['_content'].getData());
            if ($('#_form_edit_panel').validationEngine('validate') === false) return false;
            var Params = $('#_form_edit_panel').serializeObject();
            this.onCommit(TC_EVENT_CONFIG.COMMIT_URI, Params, $('#_id').val(), function(rsdata) {
                if (rsdata.result < 0) {
                    bckdialog({
                        'type': 'error'
                    }).open(rsdata.message);
                } else {
                    TC_EVENT.onRefresh();
                    TC_EVENT.onCancel();
                }
            });
            return false;
        },
        onCancel: function() {
            $('#_edit_panel_insite').hide();
            $('#viewport').show();
        },
        onDeleteItem: function(Id) {
            backend_delete(TC_EVENT_CONFIG.DELETE_URI, Id, function() {
                TC_EVENT.onRefresh();
            });
        },
        onRefresh: function() {
            oTable.fnDraw(false);
        },
        onFillter: function(v) {
            oTable.fnFilter(v, 2);
        },
        onStatusChange: function(that, Id) {
            var status = $(that).hasClass('checked') ? 'false' : 'true';
            this.onCommit(TC_EVENT_CONFIG.COMMIT_URI, {
                _status: status
            }, Id, function(rsdata) {
                if (rsdata.result < 0) {
                    bckdialog({
                        type: 'error'
                    }).open(rsdata.message);
                } else {
                    $(that).toggleClass('checked');
                    $(that).next().html(status == 'false' ? 'Disable' : 'Enable')
                }
            });
        },
        onChangeSource: function(type) {
            if (type !== TC_EVENT_CONFIG.TYPE) {
                TC_EVENT_CONFIG.TYPE = type;
                oTable.fnSettings().sAjaxSource = (TC_EVENT_CONFIG.BINDING_URI + TC_EVENT_CONFIG.TYPE);
                oTable.fnDraw(false);
            }
        }
    };
}());